﻿using System;
using System.Collections.Generic;
using System.IO;
using HumanFace;
using Stasm;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Xml;
using System.Text.RegularExpressions;
using Svg;
using Commons;
namespace Helpers
{
    public class StasmHelper
    {
        public static string rootPath = "";
        public Face GetHumanFace(string imagePath, string sDataDir, Dictionary<string, string> templates)
        {
            
            StasmOutput soutput = StamsDLLImport.AsmSearchDll(imagePath, sDataDir);
            Face hFace = null;
            if(soutput != null && soutput.CountLandmarks != null)
                hFace = new Face(GetPointList(soutput.ContainerLandmarks), templates,imagePath);
            return hFace;
        }
        private StasmOutput CallStasmImportApp(string StasmImportApp, StasmInput input)
        {
            ProcessStartInfo info = new ProcessStartInfo(StasmImportApp);
            info.Arguments = String.Format("{0} {1} {2}", input.Width, input.Height, input.Path);
            info.UseShellExecute = false;
            info.RedirectStandardOutput = true;
            Process p = Process.Start(info);
            StreamReader sr = p.StandardOutput;
            string ProgramOutput = sr.ReadToEnd();

            p.WaitForExit();
            p.Close();
            StasmOutput soutput = null;
            Regex regex = new Regex(@"\<\?xml[\d\D]*\?\>[\d\D]*\<StasmOutput[\d\D]*\>[\d\D]*\</StasmOutput\>");
            if (regex.IsMatch(ProgramOutput))
            {
                ProgramOutput = regex.Match(ProgramOutput).Captures[0].Value;
            }
            else
            {
                throw new Exception(String.Format("It has not a SteamOutput object ProgramOutput:{0}", ProgramOutput));
            }
            try
            {
                soutput = new StasmOutput();
                XmlSerializer x = new XmlSerializer(soutput.GetType());
                soutput = (StasmOutput)x.Deserialize(new StringReader(ProgramOutput));
            }
            catch (InvalidOperationException ex)
            {
                //TO-DO Exception Handle işleminde düzeltilecek
                throw new Exception(String.Format("ProgramOutput is not serializable ProgramOutput:{0}", ProgramOutput));
            }
            return soutput;
        }

        private NodeCollection GetPointList(int[] pointContainer)
        {
            NodeCollection nList = new NodeCollection();
            for (int i = 0; i < pointContainer.Length; i+=2)
            {
                Node iNode = new Node(pointContainer[i], pointContainer[i + 1],(i/2).ToString("D"));
                nList.Add(iNode);
            }
            return nList;
        }
        public static string GetSVG(Type humanFaceType)
        {
            //DB Tabloları oluşturulduğunda yeniden düzenlenecek.
            string imagePath = "ImageLibrary/";
            string document = null;
            if (!String.IsNullOrEmpty(imagePath))
                try
                {
                    SvgDocument svdoc = SvgDocument.Open(String.Format("{0}{1}{2}.svg", rootPath,imagePath, humanFaceType.Name));
                    using (StringWriter str = new StringWriter())
                    using (XmlTextWriter xml = new XmlTextWriter(str))
                    {
                        svdoc.WriteElement(xml);
                        document = str.ToString();
                    }

                }
                catch (FileNotFoundException ex)
                {
                    //Dosya bulunamadı.
                }

            return document;
        }
        private static Dictionary<string, string> GetTemplates()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add(typeof(Ear).Name, GetSVG(typeof(Ear)));
            dictionary.Add(typeof(Eye).Name, GetSVG(typeof(Eye)));
            dictionary.Add(typeof(EyeBrow).Name, GetSVG(typeof(EyeBrow)));
            dictionary.Add(typeof(Face).Name, GetSVG(typeof(Face)));
            dictionary.Add(typeof(Nose).Name, GetSVG(typeof(Nose)));
            dictionary.Add(typeof(Lips).Name, GetSVG(typeof(Lips)));
            dictionary.Add(typeof(Visage).Name, GetSVG(typeof(Visage)));
            return dictionary;
        }
    }

}
